package cn.hinglo.gameclaw.admin.action;

import cn.hinglo.gameclaw.admin.service.ArticleService;
import cn.hinglo.gameclaw.data.annotation.IgnoreToken;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.user.bean.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @createTime: 2018/7/14
 * @author: HingLo
 * @description: 文章管理
 */
@RequestMapping("admin/ar/")
@RestController
public class ArticleAction extends BaseController {
    private final ArticleService articleService;

    @Autowired
    public ArticleAction(ArticleService articleService) {
        this.articleService = articleService;
    }

    @IgnoreToken
    @GetMapping("index")
    public Result index(Long id) {
        return articleService.indexService(id);
    }

    /**
     * 文章管理
     *
     * @param page 页码
     * @param size 大小
     * @return 返回结果
     */
    @GetMapping("list")
    @IgnoreToken
    public Result list(Integer type, int page, int size) {
        return articleService.listService(type, page, size);
    }

    /**
     * 添加一篇文章
     *
     * @param article 文章对象
     * @param img     图片对象
     * @return 返回结果
     */
    @PostMapping("add")
    public Result add(Article article, @RequestParam("img") MultipartFile img) {
        return articleService.addService(article, img);
    }

    /**
     * 更新对象
     *
     * @param article 文章对象
     * @return 返回结果
     */
    @PutMapping("update")
    public Result update(Article article, MultipartFile img) {
        Long id = article.getId();
        if (id == null || id < 1) {
            return error("文章ID不正确");
        }
        return articleService.updateService(article, img);
    }

    /**
     * 删除指定的文章对象
     *
     * @param id 文章Id
     * @return 返回删除结果
     */
    @DeleteMapping("delete")
    public Result delete(Long id) {
        if (id == null || id < 1) {
            return error("文章ID不正确");
        }
        return articleService.deleteService(id);
    }

    /**
     * 文章发布状态
     *
     * @param id     文章Id
     * @param status 状态
     * @return 返回结果
     */
    @PutMapping("release")
    public Result release(Long id, boolean status) {
        if (id == null || id < 1) {
            return error("文章ID不正确");
        }
        return articleService.releaseService(id, status);
    }
}

